package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 🐱🐱🐱
 * @Date: 2025/8/18 22:10
 * @Description:
 **/
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Resource
    private BaseTrademarkMapper baseTrademarkMapper;

    @Resource
    private BaseCategoryTrademarkMapper categoryTrademarkMapper;
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //1.根据3机分类id 查询品牌分类中间表
        QueryWrapper<BaseCategoryTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = categoryTrademarkMapper.selectList(wrapper);

        //2.提取出品牌id
        if(categoryTrademarkList != null && categoryTrademarkList.size()>0){
            //3.根据品牌id集合 查询品牌集合
            List<Long> idList = categoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //4.返回品牌集合
            return baseTrademarkMapper.selectBatchIds(idList);
        }

        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //1. 关联品牌id 根据3机分类id 查询品牌分类中间表
        QueryWrapper<BaseCategoryTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = categoryTrademarkMapper.selectList(wrapper);

        //2.查询品牌的关联id
        if(categoryTrademarkList != null && categoryTrademarkList.size() >0){
            List<Long> idList = categoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //3.查询所有品牌
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);
            if(baseTrademarkList != null && baseTrademarkList.size()>0){
                //4.过滤掉关联品牌
              return baseTrademarkList.stream().filter(baseTrademark -> {
                    return !idList.contains(baseTrademark.getId());
                }).collect(Collectors.toList());
            }

        }
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if(trademarkIdList != null && trademarkIdList.size()>0){
            //遍历品牌id
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                //每次遍历过程中 创建品牌分类关联对象
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(trademarkId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());

                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            //批量新增
            this.saveBatch(baseCategoryTrademarkList);
        }
    }
}
